﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL.PageFactory.ManageHtml;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// ArticleManage 的摘要说明。
    /// </summary>
    public class ArticleManage : MarshalByRefObject
    {
        private static readonly ArticleDal Dal = new ArticleDal();

        #region 查找文章是否存在

        /// <summary>
        /// 查找文章是否存在
        /// </summary>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [ExceptionCall]
        public bool Exists(string title, out int id)
        {
            int i = Dal.Exists(title);
            if (i > 0)
            {
                id = i;
                return true;
            }
            id = 0;
            return false;
        }

        #endregion

        #region 采集文章

        /// <summary>
        /// 添加修改文章
        /// </summary>
        /// <param name="model"></param>
        /// <param name="tempFlag">null表示由文章采集等等不会有附件的方式添加的</param>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        [Logging, ExceptionCall]
        public Result Save(Article model, string tempFlag, IList<string> fields, IList<object> values)
        {
            Result resu = new Result();
            if (model.ContentTitle.Length == 0)
            {
                resu.Errors = "请添加了文章标题以后再添加！";
                return resu;
            }

            if (model.Content.Trim().Length == 0)
            {
                resu.Errors = "请填写了文章内容以后再添加！";
                return resu;
            }
            if (model.CategoryId == 0)
            {
                resu.Errors = "请选择栏目！";
                return resu;
            }
            if (CategoryManage.CheckAllowCategory(model.CategoryId) == false)
            {
                resu.Errors = "您没有该栏目的录入权限！";
                return resu;
            }

            if (string.IsNullOrEmpty(model.Author))
            {
                model.Author = "不详";
            }

            if (string.IsNullOrEmpty(model.ComeFrom))
            {
                model.ComeFrom = "不详";
            }
            else
            {
                model.ComeFrom = HTML.NoHtml(model.ComeFrom);
            }
            if (string.IsNullOrEmpty(model.Brief))
            {
                model.Brief = MyString.Left(HTML.NoHtml(model.Content.Replace("@Next@", string.Empty)), 255);
            }
            if (model.Tags.Contains("|") == false)
            {
                model.Tags = model.Tags + "|";
            }
            if (string.IsNullOrWhiteSpace(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }
            if (string.IsNullOrEmpty(model.CategoryIds)) //如果CategoryIds没有赋值就用CategoryId给它唯一的一个栏目
            {
                model.CategoryIds = model.CategoryId.ToString();
            }
            if (string.IsNullOrEmpty(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }

            if (model.Id == 0)
            {
                #region

                resu.Id = Dal.Add(model);

                HttpContext.Current.Session["Key"] = model.Tags;

                //保存Tags
                new TagsManage().Add(model, UserManage.Current.UserID);

                if (tempFlag != null) //null表示由文章采集等等不会有附件的方式添加的
                    PolicyInjection.Create<AnnexManage>().FinishAction(resu.Id, tempFlag);

                MakeSinglePage.ContentPageHTML(resu.Id);

                resu.LogMessage = "添加文章：" + model.ContentTitle;
                return resu;

                #endregion
            }

            #region

            DeleteHTML.ContentPage(model.Id);

            Dal.Edit(model);

            resu.Id = model.Id;
            HttpContext.Current.Session["Key"] = model.Tags;

            //保存Tags
            new TagsManage().Add(model, UserManage.Current.UserID);

            MakeSinglePage.ContentPageHTML(model.Id);

            resu.LogMessage = "修改文章：" + model.ContentTitle;
            return resu;

            #endregion
        }

        #endregion

        #region 采集文章

        /// <summary>
        /// 采集文章
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ExceptionCall]
        public Result Save(Article model, int userId)
        {
            Result resu = new Result();
            if (model.ContentTitle.Length == 0)
            {
                resu.Errors = "请添加了文章标题以后再添加！";
                return resu;
            }

            if (model.CategoryId < 1)
            {
                resu.Errors = "请选择栏目！";
                return resu;
            }
            if (model.Content.Trim().Length == 0)
            {
                resu.Errors = "请填写了文章内容以后再添加！";
                return resu;
            }

            if (string.IsNullOrEmpty(model.Author))
            {
                model.Author = "不详";
            }

            if (string.IsNullOrEmpty(model.ComeFrom))
            {
                model.ComeFrom = "不详";
            }
            else
            {
                model.ComeFrom = HTML.NoHtml(model.ComeFrom);
            }
            if (string.IsNullOrEmpty(model.Brief))
            {
                model.Brief = MyString.Left(HTML.NoHtml(model.Content), 255).Trim();
            }
            if (model.Tags.Contains("|") == false)
            {
                model.Tags = model.Tags + "|";
            }
            if (string.IsNullOrWhiteSpace(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }
            if (string.IsNullOrEmpty(model.CategoryIds)) //如果CategoryIds没有赋值就用CategoryId给它唯一的一个栏目
            {
                model.CategoryIds = model.CategoryId.ToString();
            }
            if (model.SEOTitle == null)
            {
                model.SEOTitle = string.Empty;
            }
            if (model.SEOKeyWord == null)
            {
                model.SEOKeyWord = string.Empty;
            }
            if (model.SEODescript == null)
            {
                model.SEODescript = string.Empty;
            }

            string picUrl =
                (from Match m in RegexHepler.Images.Matches(model.Content) select m.Groups["src"].Value).FirstOrDefault();

            model.PicUrl = picUrl ?? "";
            if (model.PicUrl.Length > 255)
            {
                model.PicUrl = "";
            }
            if (string.IsNullOrEmpty(model.PicUrl) == false)
            {
                model.IncludePic = true;
            }
            if (model.UpdateTime == DateTime.MinValue)
            {
                model.UpdateTime = DateTime.Now;
            }
            else if (model.UpdateTime > DateTime.Now)
            {
                model.UpdateTime = DateTime.Now;
            }
            if (string.IsNullOrEmpty(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }

            model.Content = HTML.NoHtml(model.Content, WebSourceTag.Note | WebSourceTag.Script);

            if (model.Id == 0)
            {
                model.Id = Dal.Add(model);
            }
            else
            {
                Dal.Edit(model);
            }

            //保存Tags
            new TagsManage().Add(model, userId);

            return resu;
        }

        #endregion

        #region 文章采集用的修改文章

        /// <summary>
        /// 文章采集用的修改文章
        /// </summary>
        /// <param name="model"></param>
        [ExceptionCall]
        public Result EditForGather(Article model)
        {
            Result resu = new Result();
            if (model.ContentTitle.Length == 0)
            {
                resu.Errors = "请添加了文章标题以后再添加！";
                return resu;
            }

            if (model.Content.Trim().Length == 0)
            {
                resu.Errors = "请填写了文章内容以后再添加！";
                return resu;
            }

            if (string.IsNullOrEmpty(model.Author))
            {
                model.Author = "不详";
            }

            if (string.IsNullOrEmpty(model.ComeFrom))
            {
                model.ComeFrom = "不详";
            }
            else
            {
                model.ComeFrom = HTML.NoHtml(model.ComeFrom);
            }

            if (string.IsNullOrEmpty(model.Brief))
            {
                model.Brief = MyString.Left(HTML.NoHtml(model.Content.Replace("@Next@", string.Empty)), 255);
            }
            if (model.Tags.Contains("|") == false)
            {
                model.Tags = model.Tags + "|";
            }
            if (string.IsNullOrEmpty(model.CategoryIds)) //如果CategoryIds没有赋值就用CategoryId给它唯一的一个栏目
            {
                model.CategoryIds = model.CategoryId.ToString();
            }
            if (string.IsNullOrEmpty(model.TemplateFileName))
            {
                model.TemplateFileName = CategoryManage.GetInfo(model.CategoryId).ShowPageTemplateFileName;
            }

            Dal.Edit(model);
            //保存Tags
            new TagsManage().Add(model, UserManage.Current.UserID);

            return resu;
        }

        #endregion

        #region 加载文章

        /// <summary>
        /// 加载文章
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Article GetInfo(int ID)
        {
            return Dal.GetInfo(ID);
        }

        #endregion

        #region 前台显示文章

        /// <summary>
        /// 前台显示文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Article PublicShow(int id)
        {
            Article info = Dal.GetInfo(id);

            if (info == null || info.IsDeleted)
            {
                return null;
            }
            return info;
        }

        #endregion
    }
}